package org.seven.jrdp.biz.core.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.seven.jrdp.biz.core.mapper.MgtCoreMapper;
import org.seven.jrdp.biz.sys.mapper.SysUserMapper;
import org.seven.jrdp.biz.sys.model.SysUser;
import org.seven.jrdp.commons.exception.BizException;
import org.seven.jrdp.commons.shiro.model.Organ;
import org.seven.jrdp.commons.shiro.model.Resource;
import org.seven.jrdp.commons.shiro.model.Role;
import org.seven.jrdp.commons.shiro.model.User;
import org.seven.jrdp.commons.shiro.service.UserService;
import org.seven.jrdp.commons.util.CipherUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@CacheConfig(cacheNames = "mgt")
@Service
public class MgtCoreService implements UserService {
	@Autowired
	private MgtCoreMapper webCoreMapper;
	@Autowired
	private SysUserMapper sysUserMapper;

	@CacheEvict(allEntries = true)
	@Transactional
	public void password(String id, String oldPassword, String newPassword) {
		SysUser record = sysUserMapper.selectByPrimaryKey(id);
		if (!record.getPassword().equals(CipherUtils.md5(oldPassword, id))) {
			throw new BizException("旧密码输入错误");
		}
		SysUser sysUser = new SysUser();
		sysUser.setId(id);
		sysUser.setPassword(CipherUtils.md5(newPassword, id));
		sysUserMapper.updateByPrimaryKeySelective(sysUser);
	}

	@Cacheable(key = "'mgt_user_'+#account")
	@Transactional
	@Override
	public User getUserByAccount(String account) {
		return webCoreMapper.getUserByAccount(account);
	}

	@Cacheable(key = "'mgt_organ_'+#userId")
	@Transactional
	@Override
	public Organ getOrganByUserId(String userId) {
		return webCoreMapper.getOrganByUserId(userId);
	}

	@Cacheable(key = "'mgt_role_'+#userId")
	@Transactional
	@Override
	public List<Role> findRoleByUserId(String userId) {
		return webCoreMapper.findRoleByUserId(userId);
	}

	@Cacheable(key = "'mgt_resource_'+#userId")
	@Transactional
	@Override
	public List<Resource> findResourceByUserId(String userId) {
		return webCoreMapper.findResourceByUserId(userId);
	}

	@Cacheable(key = "'mgt_resource'")
	@Transactional
	@Override
	public List<Resource> findResource() {
		return webCoreMapper.findResource();
	}

	@Cacheable(key = "'mgt_resource_map'")
	@Transactional
	@Override
	public Map<String, String> getResourceMap() {
		Map<String, String> map = new HashMap<String, String>();
		List<Resource> resources = findResource();
		for (Resource resource : resources) {
			String url = resource.getUrl();
			if (url != null && !"".equals(url)) {
				int indexOf = url.indexOf("?");
				if (indexOf != -1) {
					url = url.substring(0, indexOf);
				}
				String code = resource.getCode();
				String permissions = map.get(url);
				if (permissions == null) {
					map.put(url, code);
				} else {
					map.put(url, permissions + "," + code);
				}
			}
		}
		return map;
	}
}